package com.yang.utils;

import com.yang.core.TestResult;
import com.yang.core.TestResultsSet;
import java.util.*;

public class Cumpute {

    public Map<String, List> cumpute(TestResultsSet testResultsSet) {
        HashMap<String, TestResult> testResults = testResultsSet.getTestResults();
        Map<String, List> res = new HashMap<String, List>();

        for (Map.Entry<String, TestResult> entry : testResults.entrySet()) {
            TestResult testResult = entry.getValue();

            String label = entry.getKey();  //Label
            int count = testResult.getCount();  //#Sample
            ArrayList<Integer> elapsed = testResult.getElapsed();
            int[] RT = cumputrRT(elapsed);  // | Average | Median | 90%Line | 95%Line | 99%Line | Min | Max |
            float error = (float) testResult.getError() / count * 100;  // Error%

            float toalTime = (float) (testResult.getEndTime() - testResult.getStartTime()) / 1000;
            float tps = count / toalTime;  // Throughput
            float receivedBytes = testResult.getReceivedBytes() / (float) toalTime / 1024;  //Received KB/sec
            float sentBytes = testResult.getSentBytes() / (float) toalTime / 1024;  //sent KB/sec

            ArrayList<Object> objects = new ArrayList<Object>(13);
            objects.add(label);
            objects.add(count);
            for (int i = 0; i < RT.length; i++) {
                objects.add(RT[i]);
            }
            objects.add(error);
            objects.add(tps);
            objects.add(receivedBytes);
            objects.add(sentBytes);

            res.put(label, objects);
        }
        return res;
    }

    public int[] cumputrRT(ArrayList<Integer> lists) {
        Collections.sort(lists);
        int sum = 0;
        int temp1 = lists.get(0);
        int temp2 = lists.get(0);
        int max = temp1;
        int min = temp2;

        for (int i = 0; i < lists.size(); i++) {
            if (lists.get(i) > temp1) {
                max = lists.get(i);
                temp1 = max;
            }
            if (lists.get(i) < temp2) {
                min = lists.get(i);
                temp2 = min;
            }
            sum += lists.get(i);
        }

        int avg = sum / lists.size();
        int median = lists.get(lists.size() / 2);
        int ninetyPercentLine = lists.get(lists.size() * 9 / 10);
        int ninetyFivePercentLine = lists.get(lists.size() * 95 / 100);
        int ninetyNinePercentLine = lists.get(lists.size() * 99 / 100);

        int[] res = {avg, median, ninetyPercentLine, ninetyFivePercentLine, ninetyNinePercentLine, min, max};
        return res;
    }

    public static void main(String[] args) {
        int a = 101;
        System.out.println(a * 9 / 10);
    }
}
